﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace HWCWorker_Worker
{
    class MasterElectionServiceClient : ClientBase<IMasterElectionService>, IMasterElectionService
    {
        public MasterElectionServiceClient(string uri)
            : base(new NetTcpBinding(SecurityMode.None, false), new EndpointAddress(uri))
        {
            ((IContextChannel)Channel).OperationTimeout = TimeSpan.FromHours(1);
            //((ICommunicationObject)this.Channel).Faulted += new EventHandler(MasterElectionServiceClient_Faulted);
        }

        //void MasterElectionServiceClient_Faulted(object sender, EventArgs e)
        //{            
        //    ((ICommunicationObject)sender).Abort();

        //    if (sender is IMasterElectionService)
        //    {
        //        sender = this.ChannelFactory.CreateChannel(this.InnerChannel.RemoteAddress, this.InnerChannel.Via);
        //    }

        //}

        #region IMasterElectionService Members
        public void ProposeElection(string instanceID)
        {
            this.Channel.ProposeElection(instanceID);                   
        }

        public void CastElection(string instanceID, int logNumber, long logPosition)
        {
            this.Channel.CastElection(instanceID, logNumber, logPosition);
        }

        public void BroadcastVictory(List<string> chain)
        {
            this.Channel.BroadcastVictory(chain);
        }

        public bool IsRunning()
        {
            return this.Channel.IsRunning();
        }

        public string QueryMaster()
        {
            return this.QueryMaster();
        }
        #endregion
    }
}
